
Partial Class Controls_UserProfile
    Inherits System.Web.UI.UserControl
    Private _username As String = ""
    Public Property UserName() As String
        Get
            Return _username
        End Get
        Set(ByVal value As String)
            _username = value
        End Set
    End Property

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.Page.RegisterRequiresControlState(Me)
    End Sub
    Protected Overrides Sub LoadControlState(ByVal savedState As Object)
        Dim ctlState() As Object = CType(savedState, Object())
        MyBase.LoadControlState(ctlState(0))
        _username = CType(ctlState(1), String)
    End Sub

    Protected Overrides Function SaveControlState() As Object
        Dim ctlState(2) As Object
        ctlState(0) = MyBase.SaveControlState()
        ctlState(1) = _username
        Return ctlState
    End Function
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            'If the username property is an empty string, retrieve the profile for the current user, 
            'otherwise for the specified user
            Dim profile As ProfileCommon = Me.Profile
            'profile = HttpContext.Current.Profile
            If Not String.IsNullOrEmpty(Me.UserName) Then
                If Me.UserName.Length > 0 Then
                    profile = Me.Profile.GetProfile(Me.UserName)
                End If
            End If
            txtFirstName.Text = profile.FirstName
            txtLastName.Text = profile.LastName
            ddlGenders.SelectedValue = profile.Gender
            txtStreet.Text = profile.Address.Street
            txtCity.Text = profile.Address.City
            txtZip.Text = profile.Address.Zip
            txtState.Text = profile.Address.State
            txtCountry.Text = profile.Address.Country
            txtPhone.Text = profile.Contacts.Phone
            txtCell.Text = profile.Contacts.CellPhone
        End If
    End Sub
    Public Sub SaveProfile()
        'if the User name property contains an empty string, save the current user's profile
        'otherwise save the profile for the specified user
        Dim profile As ProfileCommon = Me.Profile
        If Me.UserName.Length > 0 Then
            profile = Me.Profile.GetProfile(Me.UserName)
            'Else
            '   profile = Me.Profile.GetProfile(Me.Profile.UserName)
        End If
        profile.FirstName = txtFirstName.Text
        profile.LastName = txtLastName.Text
        profile.Gender = ddlGenders.SelectedValue
        profile.Address.Street = txtStreet.Text
        profile.Address.City = txtCity.Text
        profile.Address.Zip = txtZip.Text
        profile.Address.State = txtState.Text
        profile.Address.Country = txtCountry.Text
        profile.Contacts.Phone = txtPhone.Text
        profile.Contacts.CellPhone = txtCell.Text
        profile.Save()

    End Sub

End Class
